package com.shawbs.vueblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.shawbs.vueblog.comom.Result;
import com.shawbs.vueblog.entity.Blog;
import com.shawbs.vueblog.entity.User;
import com.shawbs.vueblog.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-08-12
 */

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "获取用户列表",notes ="json串参数：page:页码，pageSize:条数，keyword:关键字")
    @PostMapping("/listPage")
    public Result listPage(@RequestBody Map<String,Object> map
    ){
        int page = map.get("page") == null ? (int) map.get("page") : 1;
        int pageSize = map.get("pageSize") == null ? (int) map.get("pageSize") : 10;
        String keyword = map.get("keyword") == null ? (String) map.get("keyword") : "";
        return userService.listPage(page, pageSize,keyword);
    }

    @ApiOperation(value = "根据ID获取用户信息")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id){
        return Result.success(userService.getById(id));
    }

    @ApiOperation(value = "根据ID获取用户信息")
    @GetMapping("/getById")
    public Result getBlog(@RequestParam(value = "id") Long id){
        return Result.success(userService.getById(id));
    }

    @RequiresAuthentication
    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody User user){
        return userService.addUser(user);
    }

    @RequiresAuthentication
    @ApiOperation(value = "编辑用户")
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody User user){
        return userService.editUser(user);
    }

    @RequiresAuthentication
    @ApiOperation(value = "批量删除用户")
    @PostMapping("/deletes")
    public Result deletes(@RequestBody Collection<Long> ids){
        return userService.removeUsers(ids);
    }

    @RequiresAuthentication
    @ApiOperation(value = "删除用户")
    @PostMapping("/delete")
    public Result deletes(@RequestParam(value = "id") Long id){
        return userService.removeUser(id);
    }

}
